home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / amok_lha / amok83.lha / WBReadArgs1.0 / WBReadArgs.mod < prev    next >
Encoding:
Text File  |  1993-08-15  |  6.3 KB  |  235 lines

  1. (*************************************************************************
  2.  
  3. :Program.    WBReadArgs.mod
  4. :Contents.   ReadArgs() for Workbench arguments
  5. :Author.     Hartmut Goebel [hG]
  6. :Address.    Aufseßplatz 5, D-8500 Nürnberg 40
  7. :Address.    UseNet: hartmut@oberon.nbg.sub.org
  8. :Address.    Z-Netz: hartmut@asn.zer   Fido: 2:246/81.1
  9. :Copyright.  Copyright © 1993 by Hartmut Goebel
  10. :Language.   Oberon
  11. :Translator. Amiga Oberon V3.0
  12. :History.    V1.0, 03 Jan 1993 [hG]
  13. :Date.       03 Jan 1993 17:03:37
  14.  
  15. (* $StackChk- $NilChk- $RangeChk- $CaseChk- $OvflChk- $ReturnChk- $ClearVars- *)
  16.  
  17. *************************************************************************)
  18.  
  19. MODULE WBReadArgs;
  20.  
  21. IMPORT
  22.   d *:= Dos,
  23.   e := Exec,
  24.   icn := Icon,
  25.   Strings,
  26.   wb *:= Workbench,
  27.   y := SYSTEM;
  28.  
  29. CONST
  30.   (* error codes *)
  31.   okay     *= 0;
  32.   otherErr *= 1;
  33.   noMemory *= 2;
  34.   noIcon   *= 3;
  35.   parsingErr *= 4;
  36.   wrongParam  *= 5;
  37.   templateErr *= 6;
  38.  
  39. VAR
  40.   error *: INTEGER;
  41.  
  42. TYPE
  43.   ToolTypes = UNTRACED POINTER TO ARRAY 2 OF e.STRPTR;
  44.   Arguments * = STRUCT
  45.     dobj -: wb.DiskObjectPtr;
  46.     longs: UNTRACED POINTER TO ARRAY OF LONGINT;
  47.   END;
  48.  
  49. VAR
  50.   dos: d.DosLibraryPtr;
  51.  
  52. PROCEDURE StrToLong *{dos,-816}(string{1}    : e.STRPTR;
  53.                                 VAR value{2} : LONGINT): LONGINT;
  54.  
  55. (*
  56. ******* WBReadArgs/ReadArgs ***********************************
  57. *
  58. *  NAME
  59. *       ReadArgs - parses tool types like Dos.ReadArgs()
  60. *
  61. *  SYNOPSIS
  62. *       ReadArgs (wbarg: wb.WBArg;
  63. *              template: ARRAY OF CHAR;
  64. *          VAR argArray: ARRAY OF y.BYTE;
  65. *              VAR args: Arguments): BOOLEAN;
  66. *
  67. *  FUNCTION
  68. *       Takes the icon associated with the workbench argument and
  69. *       parses its tool types like Dos.ReadArgs().
  70. *
  71. *       The template switches /S, /T, /N and /K are supported, /M and /A
  72. *       are not since they are useless in tool types. Only the first
  73. *       switch of each entry is observed, and the entry will be
  74. *       ignored if this is /M, /A or has no switch.
  75. *
  76. *       Make sure the template is terminated by CHR(0) (each constant
  77. *       string is).
  78. *
  79. *       Do not change the values argArray points to since they just hold
  80. *       then value returned from FindToolType(), if any.
  81. *
  82. *       Use FreeArgs() to free all memory allocated be GetArgs(). You
  83. *       must not reuse args before FreeArgs() has been called.
  84. *
  85. *  NOTES
  86. *       This function is not very smart, so you must follow the
  87. *       restictions above. But they are no handicap for using the same
  88. *       template for Dos.ReadArgs(): just put /S, /T or /N (if any) in
  89. *       front of /K and /M or /A behind.
  90. *
  91. *       Short switch syntax: ["/S"|"/N"|"/T"] ["/K"] ["/M"]["/A"]
  92. *
  93. *  INPUTS
  94. *       wbarg    - workbench argument pointer to be parsed
  95. *       template - template for parsing the tool types
  96. *       argArray - to store the results
  97. *       args     - structure to store internal
  98. *
  99. *  RESULTS
  100. *       args     - contents internal data, args.dobj points to the icon
  101. *
  102. ******)
  103.  
  104. PROCEDURE ReadArgs * (wbarg: wb.WBArg;
  105.                    template: ARRAY OF CHAR;
  106.                VAR argArray: ARRAY OF y.BYTE;
  107.                    VAR args: Arguments): BOOLEAN;
  108. VAR
  109.   argv: UNTRACED POINTER TO ARRAY 7000H OF e.APTR;
  110.   len, cnt: LONGINT;
  111.   acnt, lcnt: INTEGER;
  112.   str, str2: e.STRPTR;
  113.   c : CHAR;
  114.   oldDir: d.FileLockPtr;
  115. BEGIN
  116.   error := okay;
  117.   IF wbarg.name = NIL THEN
  118.     error := otherErr;
  119.   ELSE
  120.     oldDir := d.CurrentDir(wbarg.lock);
  121.     args.dobj := icn.GetDiskObject(wbarg.name^);
  122.     IF args.dobj = NIL THEN
  123.       error := noIcon;
  124.     ELSE
  125.       (* count /N *)
  126.       len := -1; cnt := 0;
  127.       LOOP
  128.         len := Strings.OccursPos(template,"/N",len+1);
  129.         IF len < 0 THEN EXIT; END;
  130.         INC(cnt);
  131.       END;
  132.       IF cnt # 0 THEN
  133.         y.ALLOCATE(args.longs,cnt);
  134.       END;
  135.       IF (args.longs # NIL) OR (cnt = 0) THEN
  136.         argv := y.ADR(argArray);
  137.         len := 0; lcnt := 0; acnt := 0;
  138.         WHILE len < LEN(template) DO
  139.           str := y.ADR(template[len]);
  140.           cnt := 0;
  141.           LOOP
  142.             CASE str[cnt] OF
  143.             CHR(0), "/",  ",": EXIT;
  144.             ELSE
  145.               INC(cnt);
  146.             END;
  147.           END;
  148.           c := str[cnt];
  149.           str[cnt] := CHR(0);
  150.           str2 := icn.FindToolType(args.dobj.toolTypes,str^);
  151.           str[cnt] := c;
  152.           IF c = "/" THEN (* switch *)
  153.             CASE str[cnt+1] OF
  154.             "N": (* numeric *)
  155.                  IF str2 # NIL THEN
  156.                    IF StrToLong(str2,args.longs[lcnt]) < 0 THEN
  157.                      error := wrongParam;
  158.                    ELSE
  159.                      argv[acnt] := y.ADR(args.longs[lcnt]);
  160.                    END;
  161.                  END;
  162.                  INC(lcnt); |
  163.             "K": (* keyword *)
  164.                  IF str2 # NIL THEN argv[acnt] := str2; END; |
  165.             "S": (* switch *)
  166.                  IF str2 # NIL THEN
  167.                    argv[acnt] := d.DOSTRUE;
  168.                  ELSE
  169.                    argv[acnt] := d.DOSFALSE;
  170.                  END; |
  171.             "T": (* toggle *)
  172.                  IF str2 # NIL THEN
  173.                    IF argv[acnt] = d.DOSFALSE THEN
  174.                      argv[acnt] := d.DOSTRUE;
  175.                    ELSE
  176.                      argv[acnt] := d.DOSFALSE;
  177.                    END;
  178.                  END; |
  179.             "M", "A": (* ignore *)
  180.             ELSE
  181.               error := templateErr;
  182.             END;
  183.           END;
  184.           INC(acnt); INC(cnt);
  185.           LOOP
  186.             CASE str[cnt] OF
  187.             CHR(0), ",": EXIT;
  188.             ELSE
  189.               INC(cnt);
  190.             END;
  191.           END;
  192.           INC(len,cnt+1);
  193.         END;
  194.       ELSE
  195.         error := noMemory;
  196.       END; (*args.long *)
  197.     END; (* args.dobj *)
  198.     IF  d.CurrentDir(oldDir) = NIL THEN END;
  199.   END; (* wbarg.name *)
  200.   RETURN error = okay;
  201. END ReadArgs;
  202.  
  203. (*
  204. ******* WBReadArgs/FreeArgs ***********************************
  205. *
  206. *  NAME
  207. *       FreeArgs - free memory used by GetArgs
  208. *
  209. *  SYNOPSIS
  210. *       FreeArgs * (VAR args: Arguments);
  211. *
  212. *  FUNCTION
  213. *       Frees all memory and resources allocated when calling
  214. *       GetArgs().
  215. *
  216. *       args may be used for another call of GetArgs() afterwards.
  217. *
  218. **********)
  219.  
  220. PROCEDURE FreeArgs * (VAR args: Arguments);
  221. BEGIN
  222.   IF args.dobj # NIL THEN icn.FreeDiskObject(args.dobj) END;
  223.   (* $IFNOT GarbaceCollector *)
  224.     DISPOSE (args.longs);
  225.   (* $ELSE *)
  226.     args.longs := NIL;
  227.   (* $END *)
  228.   args.dobj := NIL;
  229. END FreeArgs;
  230.  
  231. BEGIN
  232.   dos := d.dos;
  233.  
  234. END WBReadArgs.
  235.